﻿using PhotoGallery.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace PhotoGallery.Data.Repository
{
    public class ExternalLoginRepository : BaseRepository
    {
        const string xmlData = "\\App_Data\\OAuthData.xml";
        string fileName;
        XDocument doc;
        List<ExternalLogin> externalLogins  = new List<ExternalLogin>();

        public ExternalLoginRepository()
        {
            //var filesDir = HttpContext.Current.Server.MapPath("~");
            fileName = BasePath + xmlData;
            doc = XDocument.Load(fileName);
            var qry = from login in doc.Descendants("externalLogin")
                      select new ExternalLogin(
                                      login.Element("provider").Value,
                                      login.Element("providerUserId").Value,
                                      login.Element("providerDisplayName").Value,
                                      login.Element("localUsername").Value
                                      );
            externalLogins.AddRange(qry.ToList<ExternalLogin>());
        }

        public List<ExternalLogin> Get()
        {
            return externalLogins;
        }

        public ExternalLogin Get(string providerUserId)
        {
            return externalLogins.FirstOrDefault(l => l.ProviderUserId.Equals(providerUserId));
        }

        public ExternalLogin Get(string provider, string localUsername)
        {
            return externalLogins.FirstOrDefault(l => l.Provider.Equals(provider) && l.LocalUsername.Equals(localUsername));
        }

        public IEnumerable<ExternalLogin> GetByUsername(string localUsername)
        {
            return externalLogins.Where(l => l.LocalUsername.Equals(localUsername));
        }

        public bool Create(ExternalLogin login)
        {
            try
            {
                externalLogins.Add(login);

                XElement xml = new XElement("externalLogins",
                from i in externalLogins
                //orderby login.Provider.ID
                select new XElement("externalLogin",
                          new XElement("provider", i.Provider),
                          new XElement("providerUserId", i.ProviderUserId),
                          new XElement("providerDisplayName",i.ProviderDisplayName),
                          new XElement("localUsername", i.LocalUsername)
                          )
                );
                
                XDocument doc = new XDocument(xml);
                doc.Save(fileName);
            }
            catch (Exception ex)
            {
                throw new Exception("", ex.InnerException);
            }

            return true;
        }

        public void Delete(string localUserName, string provaider)
        {
            ExternalLogin login = externalLogins.FirstOrDefault(a => a.LocalUsername == localUserName && a.Provider == provaider);
            externalLogins.Remove(login);
            doc.Root.Elements("externalLogin").Where(i => i.Element("localUsername").Equals(localUserName)
                                                            && i.Element("provider").Equals(provaider)).Remove();
            doc.Save(fileName);
        }
    }
}